package com.taxinfo.view.declaration;

import com.taxinfo.component.progress.TableProgressIndicator;
import com.taxinfo.component.table.ConditionTable;
import com.taxinfo.pojo.dto.DecDocumentRecDto;
import com.taxinfo.service.DecDocumentRecService;
import com.taxinfo.utils.SpringUtil;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.control.Label;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 *  申报文档记录
 *
 * @author wanghy
 */
public class DecDocumentTable extends ConditionTable<DecDocumentRecDto> {

    private final Logger logger = Logger.getLogger(DecDocumentTable.class);

    private static final String MENU_CODE = "decRecord";

    private final DecDocumentRecService decDocumentRecService = (DecDocumentRecService) SpringUtil.getBean("DecDocumentRecServiceImpl");

    private final ObservableList<DecDocumentRecDto> items = getItems();


    public DecDocumentTable(List<DecDocumentRecDto> items) {
        super(MENU_CODE, items, DecDocumentRecDto.class);
        this.items.addAll(items);
        showAllInfos();
    }


    /**
     *
     */
    public void showAllInfos() {
        clearData();
        setPlaceholder(new TableProgressIndicator());

        Task<List<DecDocumentRecDto>> task = new Task<List<DecDocumentRecDto>>() {
            @Override
            protected List<DecDocumentRecDto> call() {
                List<DecDocumentRecDto> beans = decDocumentRecService.selectAllData();
                if (beans != null)
                    return beans;
                else
                    return new ArrayList<>(0);
            }
        };
        new Thread(task).start();
        task.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.size() != 0)
                items.addAll(newValue);
            else
                setPlaceholder(new Label("没有数据"));
        });
    }

    /**
     * 添加用户信息
     *
     * @param dto 用户信息
     */
    public void addBean(DecDocumentRecDto dto) {
        items.add(dto);
    }

}
